package com.xuan.modules.system.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuan.commons.api.R;
import com.xuan.commons.config.jwt.JwtConfig;
import com.xuan.commons.utils.Sha256;
import com.xuan.modules.system.entity.User;
import com.xuan.modules.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 七月初七
 * @since 2021-05-21
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户接口",tags = "用户接口")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    @ApiOperation(value = "用户注册",notes = "传入用户对象")
    public R register(@RequestBody User user){
        try {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",user.getUsername());
            User user1 = userService.getOne(queryWrapper);
            if(user1!=null){
                return R.error("已存在该用户");
            }
            userService.register(user);
            return R.ok("注册成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("注册失败");
        }
    }
    @GetMapping("/login")
    @ApiOperation(value = "登录",notes = "登录")
    public R login (HttpServletRequest request,String username,String password){
        if(StringUtils.isEmpty(username) || "".equals(username) || "null".equals(username)){
            return R.error("用户名不能为空");
        }
        if(StringUtils.isEmpty(password) || "".equals(password) || "null".equals(password)){
            return R.error("密码不能为空");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userService.getOne(queryWrapper);
        if(user==null){
            return R.error("该用户不存在,请注册");
        }
        String salt = user.getSalt();//获取到用户的盐值
        String pwd=password+salt;
        String newPwd = Sha256.encodePassword(pwd);
        if(!newPwd.equals(user.getPassword())){
            return R.error("密码错误");
        }
        String token = JwtConfig.getToken(user.getUsername(), user.getPassword());
        request.getSession().setAttribute("token",token);
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOpt("token",token);
        jsonObject.putOpt("user",user);
        return R.date(jsonObject);
    }

    @GetMapping("/getUser")
    @ApiOperation(value = "获取用户",notes = "获取用户")
    public R getUser(){
        List<User> users = userService.list();
        return R.date(users);
    }
}

